<script setup>
import {ref} from 'vue'

//添加订单数据模型
const orderModel = ref({
  //coverImg:'',   //订单封面
  orderId: '',   //订单编号
  flowerId: '',  //花卉编号
  orderStatus:'',  //订单状态
  flowerQuantity: '',  //鲜花数量
  price:'',     //订单价格
  orderTime:''  //下单时间-订单创建时间
})

//状态
const statusOptions = {
  '0': '未支付',
  '1': '未接单',
  '2': '待发货',
  '3': '已发货',
  '4': '已完成',
  '5': '已取消',
}

//订单列表数据模型
const orders = ref([])

//分页条数据模型
const pageNum = ref(1)//当前页
const total = ref(0)//总条数
const pageSize = ref(3)//每页条数

//用户搜索时选中的订单编号
const searchOrderId=ref('')

//用户搜索时订单的发布状态
const searchOrderStatus=ref('')

//当每页条数发生了变化，调用此函数
const onSizeChange = (size) => {
  pageSize.value = size
  orderList();
}
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
  pageNum.value = num
  orderList();
}

import {orderListService} from "@/api/order.js";
import {ElMessage} from "element-plus";
// 获取订单列表
const orderList = async () => {
  loading.value = true // 开始加载
  try {
    const params = {
      pageNum: pageNum.value,
      pageSize: pageSize.value,
      orderId: searchOrderId.value,
      status: searchOrderStatus.value
    }

    const result = await orderListService(params)

    orders.value = result.data.items.map(order => ({
      ...order,
      orderStatus: statusOptions[order.orderStatus] || '未知状态',
      orderTime: formatDate(order.orderTime)
    }))

    total.value = result.data.total
  } catch (error) {
    ElMessage.error('获取订单列表失败: ' + error.message)
  } finally {
    loading.value = false // 结束加载
  }
}
orderList();

// 日期格式化函数
const formatDate = (timestamp) => {
  if (!timestamp) return ''
  const date = new Date(timestamp)
  return date.toLocaleDateString() + ' ' + date.toLocaleTimeString()
}

// 搜索重置
const resetSearch = () => {
  searchOrderId.value = ''
  searchOrderStatus.value = ''
  pageNum.value = 1  // 重置页码
  orderList()
}

const loading = ref(false)

</script>

<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>我的订单</span>
      </div>
    </template>

    <!-- 搜索表单 -->
    <el-form inline>
      <el-form-item label="订单编号：">
        <el-input
            v-model="searchOrderId"
            placeholder="请输入订单编号"
            clearable
        />
      </el-form-item>

      <el-form-item label="订单状态：">
        <el-select
            v-model="searchOrderStatus"
            placeholder="全部状态"
            clearable
        >
          <el-option
              v-for="(label, value) in statusOptions"
              :key="value"
              :label="label"
              :value="value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="orderList">搜索</el-button>
        <el-button @click="resetSearch">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 订单表格（修正点4：完整的loading配置） -->
    <el-table
        :data="orders"
        v-loading="loading"
        element-loading-text="数据加载中..."
        element-loading-background="rgba(255, 255, 255, 0.8)"
        element-loading-spinner="el-icon-loading"
    >
      <el-table-column label="订单编号" prop="orderId" width="180"/>
      <el-table-column label="鲜花编号" prop="flowerId" width="120"/>
      <el-table-column label="订单状态" prop="orderStatus" width="120"/>
      <el-table-column label="数量" prop="flowerQuantity" width="80"/>
      <el-table-column label="价格" prop="price" width="120">
        <template #default="{ row }">
          ￥{{ (row.price / 100).toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column label="下单时间" prop="orderTime" width="180"/>
    </el-table>

    <!-- 分页组件 -->
    <div class="pagination-wrapper">
      <el-pagination
          v-model:current-page="pageNum"
          v-model:page-size="pageSize"
          :page-sizes="[3, 5, 10, 20]"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="onSizeChange"
          @current-change="onCurrentChange"
      />
    </div>
  </el-card>
</template>

<style scoped>
.page-container {
  min-height: 100%;

  .header {
    font-size: 18px;
    font-weight: 600;
  }
}

.pagination-wrapper {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.el-form-item {
  margin-right: 20px;
}

.el-select {
  width: 120px;
}

.el-input {
  width: 200px;
}
</style>